package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.text.Convert;

/**
 * 系统用户信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-05-22
 */
@Service
public class SysUserServiceImpl implements ISysUserService
{
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private SysRoleMapper roleMapper;
    /**
     * 查询系统用户信息
     * 
     * @param uId 系统用户信息ID
     * @return 系统用户信息
     */
    @Override
    public SysUser selectSysUserById(Long uId)
    {
        return userMapper.selectSysUserById(uId);
    }

    @Override
    public SysUser selectUserByLoginName(String LoginName,String LoginPawd) {
        return userMapper.selectSysUserByLoaginName(LoginName,LoginPawd);
    }

    /**
     * 查询系统用户信息列表
     * 
     * @param zxSysuser 系统用户信息
     * @return 系统用户信息
     */
    @Override
    public List<SysUser> selectSysUserList(SysUser zxSysuser)
    {
        return userMapper.selectSysUserList(zxSysuser);
    }

    /**
     * 新增系统用户信息
     * 
     * @param zxSysuser 系统用户信息
     * @return 结果
     */
    @Override
    public int insertSysUser(SysUser zxSysuser)
    {
         userMapper.insertSysUser(zxSysuser);
        int i = userMapper.insertRoleUser(zxSysuser.getuId(), zxSysuser.getSrId());
        return  i;
    }

    /**
     * 修改系统用户信息
     * 
     * @param zxSysuser 系统用户信息
     * @return 结果
     */
    @Override
    public int updateSysUser(SysUser zxSysuser)
    {
        return userMapper.updateSysUser(zxSysuser);
    }

    /**
     * 删除系统用户信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysUserByIds(String ids)
    {
        int i = userMapper.deleteSysUserByIds(Convert.toStrArray(ids));
        if (i==1){
            return  userMapper.deleteRoleUserByIds(Convert.toStrArray(ids));
        }else {
            return 0;
        }
    }

    /**
     * 删除系统用户信息信息
     * 
     * @param uId 系统用户信息ID
     * @return 结果
     */
    @Override
    public int deleteSysUserById(Long uId)
    {
    return 1;

    }

    @Override
    public String checkPhoneUnique(String phone) {
        SysUser info = userMapper.checkPhoneUnique(phone);
        if (StringUtils.isNotNull(info))
        {
            return UserConstants.USER_PHONE_NOT_UNIQUE;
        }
        return UserConstants.USER_PHONE_UNIQUE;

    }

    @Override
    public String checkLoginNameUnique(String loginName) {
            int i = userMapper.checkLoginNameUnique(loginName);
        if (i>0)
        {
            return UserConstants.USER_PHONE_NOT_UNIQUE;
        }
        return UserConstants.USER_PHONE_UNIQUE;
    }

    @Override
    public void checkUserAllowed(SysUser user) {
        if (StringUtils.isNotNull(user.getuId()) && user.isAdmin())
        {
            throw new BusinessException("不允许操作超级管理员用户");
        }
    }

    @Override
    public int changeStatus(SysUser user) {
        return userMapper.updateSysUser(user);
    }

    @Override
    public int resetUserPwd(SysUser user) {
        return userMapper.updateSysUser(user);
    }

    @Override
    public boolean registerUser(SysUser user) {
        return false;
    }

    @Override
    public int updateUserInfo(SysUser user) {
        return userMapper.updateSysUser(user);
    }

}
